﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace mytest2.webs
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string domainAndName = Page.User.Identity.Name;
            //string domainAndName = "sol.shuion.com.cn\\vs-movit05";
            //string domainAndName = "asd";
            String redirectUrl = Page.Request.QueryString["redirectUrl"];
            Debug.WriteLine(redirectUrl);
             
            if (redirectUrl == null || redirectUrl== "")
            {
                Response.Write("未知参数 redirectUrl:" + redirectUrl + "，请联系管理员");
                return;
            }
            string[] infoes = domainAndName.Split(new char[1] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            string userDomainName = "";
            string userName = "";
            if (infoes.Length > 1)
            {
                userDomainName = infoes[0];
                userName = infoes[1];
            }

            WriteTextLog("OEMS AD登录信息收集：", userDomainName + "|" + userName, DateTime.Now);
            if (userDomainName != "" && userName != "")
            {
                Debug.WriteLine(Request.Url.ToString());
                long timestamp =(DateTimeOffset.Now.ToUnixTimeSeconds());

                string md5bef = "secure"+timestamp+ userName; //加密前数据
                string md5= System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(md5bef, "MD5"); //加密后数据
                String paramsNew = "userDomainName=" + userDomainName + "&userName=" + userName + "&timestamp=" + timestamp + "&md5=" + md5;

                String redirectUrlNew = parseUrl(redirectUrl, paramsNew);
                Response.Redirect(redirectUrlNew);
                //Response.Redirect(redirectUrl + "?userDomainName=" + userDomainName + "&userName=" + userName + "&timestamp=" + timestamp + "&md5=" + md5, false);
            }
            else
            {
                //未获取到域账号，理论上不可能
                Response.Write("未获取到域账号，请联系管理员");
                return;

            }
           

        }

        public static void WriteTextLog(string action, string strMessage, DateTime time)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"LogMobileInterfaceLog\";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
            StringBuilder str = new StringBuilder();
            str.Append("Time:    " + time.ToString() + "\r\n");
            str.Append("Action:  " + action + "\r\n");
            str.Append("Message: " + strMessage + "\r\n");
            str.Append("-----------------------------------------------------------\r\n\r\n");
            StreamWriter sw;
            if (!File.Exists(fileFullPath))
            {
                sw = File.CreateText(fileFullPath);
            }
            else
            {
                sw = File.AppendText(fileFullPath);
            }
            sw.WriteLine(str.ToString());
            sw.Close();
        }

        public String parseUrl(String url, String paramOther)
        {
            String urlTemp = url.Replace("#", "qazwsxedc");
            Uri urlObj = new Uri(urlTemp);

            String urlObjQuery = urlObj.Query;
            if (urlObjQuery == null || urlObjQuery == "")
            {
                return url + "?" + paramOther;
            }

            return url + "&" + paramOther;
        }
    }
}